//
//  EBMBondFollowViewController.m
//  eBondMall
//
//  Created by 赵世杰 on 2020/4/5.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#import "EBMBondFollowViewController.h"
#import "EBMBondFollowTableViewCell.h"
#import "EBMBondOperationRequestObj.h"
#import "EBMHomeGuessYourLikeClass.h"
#import "EBMCustomerAlertView.h"


@interface EBMBondFollowViewController ()

@end

@implementation EBMBondFollowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //  初始化变量
    [self initData];
    
    //  初始化视图
    [self initView];
    
    //  初始化网络请求
    [self requestData];
    
}


#pragma mark ----------初始化变量----------
//  初始化变量
- (void)initData
{
    
}



#pragma mark ----------初始化视图----------
//  初始化视图
- (void)initView
{
    
    //  设置导航栏内容
    [self setNavigationContent];
    
    //  创建控件    按钮设置
    [self createViewInButton];
    
    //  创建控件    TableView
    [self createViewInTableView];
    
}


//  设置导航栏内容
- (void)setNavigationContent
{
    self.navigationItem.title = @"债券关注";
    
    //  创建控件    右侧按钮
    [self createViewInRightButton];
    
}

//  创建控件    右侧按钮
- (void)createViewInRightButton
{
    NSString *rightString = self.typeIndex == 0 ? @"编辑":@"取消";
    [self baseCreateViewInNavigationBarRightButtonWtihTitle:rightString imageName:nil buttonClickBlock:^{
        
        if(self.typeIndex == 0)
        {
            self.tableViewBottomLayout.constant = 50+8;
            self.bottomBgView.hidden = NO;
            
            
            //  设置类型状态
            [self settingSelectButtonTypeStatusWithIsSelect:NO];
            
            //  检测选中按钮的状态
            [self checkSelectButtonStatus];
            
        }
        else
        {
            self.tableViewBottomLayout.constant = 0;
            self.bottomBgView.hidden = YES;
        }
       
        self.typeIndex = self.typeIndex==0 ? 1:0;
        
        [self.tableView reloadData];
        
        //  创建控件    右侧按钮
        [self createViewInRightButton];
        
    }];
}

//  创建控件    按钮设置
- (void)createViewInButton
{
    self.removeButton.layer.borderWidth = 1;
    self.removeButton.layer.borderColor = RGBCOLOR16_E34040.CGColor;
    
}

//  创建控件    TableView
- (void)createViewInTableView
{
    [self.tableView registerNibWithCellClassName:@"EBMBondFollowTableViewCell"];
    
    
    
    __weak typeof(self) weakSelf = self;
    
    [self.tableView loadTableMjHeaderWithHeadrRefreshingBlock:^{
        
        //  网络请求    关注
        [weakSelf requestDataInFollowWithIsDown:YES page:1];
        
    } footerRefreshingBlock:^{
        
        //  网络请求    关注
        [weakSelf requestDataInFollowWithIsDown:NO page:weakSelf.basePageIndex];
        
    }];
    
}

#pragma mark ----------初始化网络请求----------

- (void)baseRefreshRequestData
{
    //  初始化网络请求
    [self requestData];
    
}

//  初始化网络请求
- (void)requestData
{
    //  网络请求    关注
    [self requestDataInFollowWithIsDown:YES page:1];
    
}


//  网络请求    关注
- (void)requestDataInFollowWithIsDown:(BOOL)isDown page:(NSInteger)page
{
    NSString *urlString = Myself_member_myConcerns;
    
    NSDictionary *parameters_dic = @{@"pageNum":@(page),
                                     @"pageSize":@(20)
    };
    
    [EBMRequest GETRequestTakeDataWithUrlString:urlString headerDic:nil parameters:parameters_dic loadView:self.view successBlock:^(id result) {
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            NSArray *array = [[NSArray alloc]init];
            
            if([result isKindOfClass:[NSArray class]])
            {
                array = [SJDataManageTool arrayModelDateAssemblyWithResult:result modelName:@"EBMShopOneBondModel"];
            }
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self loadRequestSuccessHandleDataWithIsDown:isDown];
                
                [self.baseDataMArray addObjectsFromArray:array];

                [self.tableView reloadData];

                //  检测选中按钮的状态
                [self checkSelectButtonStatus];
                
            });
            
        });
        
        
    } serverErrorBlock:^(id result) {
        
        [self.tableView reloadData];
        
        [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:HttpUrlResultMessageError];
        
    } failBlock:^{
        
        [self.tableView reloadData];
        
    }];
    
    
}

//  网络请求    删除关注
- (void)requestDataInRemoveFollow
{
    NSArray *select_array = [self checkSelectButtonStatus];
    
    NSMutableArray *array_m = [[NSMutableArray alloc]init];
    
    for (EBMShopOneBondModel *model in select_array)
    {
        [array_m addObject:model.iId];
        
    }
    
    
    [EBMBondOperationRequestObj requestRemoveCollectionBondWithBondArray:array_m successBlock:^{
        
        [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:@"删除成功"];
        
        [self.tableView.mj_header beginRefreshing];
        
        
    } failBlock:nil];
    
    
}



#pragma mark --------------响应方法--------------



//  检测选中按钮的状态
- (NSArray *)checkSelectButtonStatus
{
    
    NSMutableArray *array_m = [[NSMutableArray alloc]init];
    
    for (EBMShopOneBondModel *model in self.baseDataMArray) {
        if(model.isSelect == YES)
        {
            [array_m addObject:model];
        }
    }
    
    self.selectButton.selected = array_m.count == self.baseDataMArray.count ? YES:NO;
    
    [self.removeButton setTitle:[NSString stringWithFormat:@"删除(%@只)",@(array_m.count)] forState:UIControlStateNormal];
    
    return array_m;
    
}

//  设置类型状态
- (void)settingSelectButtonTypeStatusWithIsSelect:(BOOL)isSelect
{
    for (EBMShopOneBondModel *model in self.baseDataMArray)
    {
        model.isSelect = isSelect;
    }
    
    [self.tableView reloadData];

    self.selectButton.selected = isSelect;
    
}


#pragma mark --------------响应方法--------------


//  全选
- (IBAction)selectButtonClick:(id)sender {
    
    UIButton *button = sender;
    
    //  设置类型状态
    [self settingSelectButtonTypeStatusWithIsSelect:!button.selected];
    
    //  检测选中按钮的状态
    [self checkSelectButtonStatus];
    
}

- (IBAction)removeButtonClick:(id)sender {
    
     NSArray *select_array = [self checkSelectButtonStatus];
    
    
    EBMCustomerAlertView *alertView = [[EBMCustomerAlertView alloc]initWithContent:[NSString stringWithFormat:@"确定删除这%@只债券吗？",@(select_array.count)] textAlignment:NSTextAlignmentCenter left:@"取消" right:@"确定" leftClickBlock:nil rightClickBlock:^{
        
        
        //  网络请求    删除关注
        [self requestDataInRemoveFollow];
        
    }];
    
    [alertView addToWindow];
    
    
    
}

#pragma mark ----------UITableViewDelegate 代理方法----------


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.baseDataMArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EBMBondFollowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EBMBondFollowTableViewCell" forIndexPath:indexPath];
    
    cell.typeIndex = self.typeIndex;
    
    __weak typeof(self) weakSelf = self;
    
    EBMShopOneBondModel *model = self.baseDataMArray[indexPath.row];
    
    cell.model = model;
    
    cell.operationBlock = ^{
        
        model.isSelect = !model.isSelect;
        
        [weakSelf.tableView reloadData];
        
        //  检测选中按钮的状态
        [weakSelf checkSelectButtonStatus];
        
        
    };
    
    [cell setNeedsLayout];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 114+8;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [EBMHomeGuessYourLikeClass guessYourLikeTableViewdidSelectWithModel:self.baseDataMArray[indexPath.row] viewController:self];
    
}


@end
